// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flowerptl:[],
    flowernew:[],
    flowertime:[],
    flowerspl:[],
    flowergift:[],

    style:['now',''],
      hidden:[false,true,true],
    // 倒计时
    windowHeight: 654,
   maxtime: "",
   isHiddenLoading: true,
   isHiddenToast: true,
   dataList: {},
   countDownDay: 0,
   countDownHour: 0,
   countDownMinute: 0,
   countDownSecond: 0,
  },
  //事件处理函数
  bindViewTap: function() {
  wx.navigateTo( {
   url: '../logs/logs'
  })
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFlowerPtl(),
    this.getFlowerNew(),
    this.getFlowerTime(),
    this.getFlowerSpl(),
    this.getFlowerGift(),
    this.setData( {
      windowHeight: wx.getStorageSync( 'windowHeight' )
     });
  },

  getFlowerGift:function(){
      var that = this
      const db = wx.cloud.database()
      db.collection('flower').where({
      genre:db.RegExp({
        regexp:'周边',
        regexp:'礼品',
        options:'i'
      })
    }).limit(6).get({
      success:function(res){
       
        that.setData({
          flowergift:res.data
        })
      },
      fail:function(err){
        console.log(err)
    }
  })
  },

  getFlowerSpl:function(){
    var that = this
   const db = wx.cloud.database()
   db.collection('flower').orderBy('productsell','asc').get({
       success:function(res){
        
         that.setData({
           flowerspl:res.data
         })
       },
       fail:function(err){
         console.log(err)
       }
     })
 },

  getFlowerTime:function(){
    var that = this
   const db = wx.cloud.database()
   db.collection('flower').orderBy('productsell','asc').limit(2).get({
       success:function(res){
         
         that.setData({
           flowertime:res.data
         })
       },
       fail:function(err){
         console.log(err)
       }
     })
 },

  getFlowerPtl:function(){
     var that = this
    const db = wx.cloud.database()
    db.collection('flower').orderBy('productsell','desc').limit(5).get({
        success:function(res){
         
          that.setData({
            flowerptl:res.data
          })
        },
        fail:function(err){
          console.log(err)
        }
      })
  },

  getFlowerNew:function(){
    var that = this
    const db = wx.cloud.database()
    db.collection('flower').where({
    genre:db.RegExp({
      regexp:'长辈',
      options:'i'
    })
  }).limit(6).get({
    success:function(res){
     
      that.setData({
        flowernew:res.data
      })
    },
    fail:function(err){
      console.log(err)
  }
})
},

// 倒计时
onReady: function () {
  var totalSecond = Date.parse(new Date("2021/7/12")) / 1000 - Date.parse(new Date()) / 1000;
  var interval = setInterval(function () {
   // 秒数
   var second = totalSecond;
   // 天数位
   var day = Math.floor(second / 3600 / 24);
   var dayStr = day.toString();
   if (dayStr.length == 1) dayStr = '0' + dayStr;
   // 小时位
   var hr = Math.floor((second - day * 3600 * 24) / 3600);
   var hrStr = hr.toString();
   if (hrStr.length == 1) hrStr = '0' + hrStr;
   // 分钟位
   var min = Math.floor((second - day * 3600 *24 - hr * 3600) / 60);
   var minStr = min.toString();
   if (minStr.length == 1) minStr = '0' + minStr;
   // 秒位
   var sec = second - day * 3600 * 24 - hr * 3600 - min*60;
   var secStr = sec.toString();
   if (secStr.length == 1) secStr = '0' + secStr;
   this.setData({
    countDownDay: dayStr,
    countDownHour: hrStr,
    countDownMinute: minStr,
    countDownSecond: secStr,
   });
   totalSecond--;
   if (totalSecond < 0) {
    clearInterval(interval);
    wx.showToast({
     title: '活动已结束',
    });
    this.setData({
     countDownDay: '00',
     countDownHour: '00',
     countDownMinute: '00',
     countDownSecond: '00',
    });
   }
  }.bind(this), 1000);
 },
 //cell事件处理函数
 bindCellViewTap: function (e) {
  var id = e.currentTarget.dataset.id;
  wx.navigateTo({
   url: '../babyDetail/babyDetail?id=' + id
  });
 },


 click1(env) {
  let index=env.target.dataset.index;
  let style=this.data.style;
  let hidden =this.data.hidden;

  for(let i=0;i<3;i++){
    if(i==index){
      style[i]="now";
      hidden[i]=false;
    }else{
      style[i]="";
      hidden[i]=true;
    }
  }
  this.setData({
    style,
    hidden
  })
},

})